package com.aaa.spring.controller;


import com.aaa.spring.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

//标识这是个控制器 控制跳转的
@Controller
@RequestMapping("/home")
public class HomeController {
    //url请求路径 使用参数
    @RequestMapping("/index")
    public ModelAndView home(Integer id,
                             //当前参数要求必传 required = true
                             @RequestParam(required = true,defaultValue = "老王") String name
    ){
        //ModelAndView 携带数据及视图信息
        ModelAndView mv = new ModelAndView();
        //添加返回数据
        mv.addObject("id", id);
        mv.addObject("name", name);
        //设置视图
        mv.setViewName("home");
        return mv;
    }
    //url请求路径 使用实体对象接收参数
    @RequestMapping("/index2")
    public ModelAndView index2(User params){
        //ModelAndView 携带数据及视图信息
        ModelAndView mv = new ModelAndView();
        //添加返回数据
        mv.addObject("id", params.getId());
        mv.addObject("name", params.getName());
        //设置视图
        mv.setViewName("home");
        return mv;
    }
    //url请求路径 使用request对象 获取参数 很少使用
    @RequestMapping("/index1")
    public ModelAndView index1(HttpServletRequest request){
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        //ModelAndView 携带数据及视图信息
        ModelAndView mv = new ModelAndView();
        //添加返回数据
        mv.addObject("id", id);
        mv.addObject("name", name);
        //设置视图
        mv.setViewName("home");
        return mv;
    }
}
